package com.jason.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jason.system.common.util.Pager;
import com.jason.system.common.util.StringUtil;
import com.jason.system.entity.Remark;
import com.jason.system.entity.SignForm;
import com.jason.system.entity.Student;
import com.jason.system.entity.Tool;
import com.jason.system.mapper.RemarkMapper;
import com.jason.system.mapper.SignFormMapper;
import com.jason.system.mapper.StudentMapper;
import com.jason.system.mapper.ToolMapper;
import com.jason.system.service.SignFormService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author jie
 * @version 1.0
 * success come from self-discipline
 * @date 2021/4/8 11:22
 */
@Service
public class SignFormServiceImpl implements SignFormService {

  @Resource
  SignFormMapper signFormMapper;

  @Resource
  StudentMapper studentMapper;

  @Resource
  ToolMapper toolMapper;

  @Resource
  RemarkMapper remarkMapper;

  @Override
  public Pager<SignForm> getAllSighForms(Integer page, Integer size) {
    PageHelper.startPage(page,size);
    List<SignForm> signFormList = signFormMapper.getAllSighForms();
    for (SignForm sf : signFormList){
      //获取值班学生主键id,List<Integer>
      List<Integer> operatorStuIdList = Arrays.stream(sf.getOperators().split(",")).map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
      List<Student> operatorStudent = studentMapper.getStudentByList(operatorStuIdList);
      sf.setOperatorStudents(operatorStudent);
      //获取旷班学生主键id,List<Integer>
      List<Integer> absenteeismStuIdList = Arrays.stream(sf.getAbsenteeism().split(",")).map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
      List<Student> absenteeismStudent = studentMapper.getStudentByList(absenteeismStuIdList);
      sf.setAbsenteeismStudents(absenteeismStudent);
      //获取工具主键id,List<Integer>
      List<Integer> toolsIdList = Arrays.stream(sf.getTools().split(",")).map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
      List<Tool> tools = toolMapper.getToolByIds(toolsIdList);
      sf.setToolList(tools);
      //获取备注交接信息主键id
      List<Integer> remarkIdList = Arrays.stream(sf.getRemarks().split(",")).map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
      List<Remark> remarkList = remarkMapper.getRemarkByIds(remarkIdList);
      sf.setRemarkList(remarkList);
      sf.setSuccessors(studentMapper.getStudentById(sf.getSuccessorId()));
    }
    PageInfo<SignForm> signFormPageInfo = new PageInfo<>(signFormList);
    return new Pager<>(signFormPageInfo);
  }

  @Override
  public SignForm getSignFormById(Integer sfId) {
    SignForm signForm = signFormMapper.getSignFormById(sfId);
    //获取值班学生主键id,List<Integer>
    List<Integer> operatorStuIdList = Arrays.stream(signForm.getOperators().split(",")).map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
    List<Student> operatorStudent = studentMapper.getStudentByList(operatorStuIdList);
    signForm.setOperatorStudents(operatorStudent);
    //获取旷班学生主键id,List<Integer>
    List<Integer> absenteeismStuIdList = Arrays.stream(signForm.getAbsenteeism().split(",")).map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
    List<Student> absenteeismStudent = studentMapper.getStudentByList(absenteeismStuIdList);
    signForm.setAbsenteeismStudents(absenteeismStudent);
    //获取工具主键id,List<Integer>
    List<Integer> toolsIdList = Arrays.stream(signForm.getTools().split(",")).map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
    List<Tool> tools = toolMapper.getToolByIds(toolsIdList);
    signForm.setToolList(tools);
    List<Integer> remarkIdList = Arrays.stream(signForm.getRemarks().split(",")).map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
    List<Remark> remarkList = remarkMapper.getRemarkByIds(remarkIdList);
    signForm.setRemarkList(remarkList);
    signForm.setSuccessors(studentMapper.getStudentById(signForm.getSuccessorId()));
    return signForm;
  }

  @Override
  public void insertSignForm(SignForm signForm) {
    //将学生主键id，以“,”分割,字符串形式储存
    StringUtil stringUtil = new StringUtil();
    signForm.setOperators(stringUtil.stuListToString(signForm.getOperatorStudents()));
    signForm.setAbsenteeism(stringUtil.stuListToString(signForm.getAbsenteeismStudents()));
    signForm.setTools(stringUtil.toolListToString(signForm.getToolList()));
    signForm.setRemarks(stringUtil.remarkListToString(signForm.getRemarkList()));
    signForm.setSuccessorId(signForm.getSuccessors().getSId());
    signFormMapper.insertSignForm(signForm);
  }

  @Override
  public void updateSignForm(SignForm signForm) {
    StringUtil stringUtil = new StringUtil();
    signForm.setOperators(stringUtil.stuListToString(signForm.getOperatorStudents()));
    signForm.setAbsenteeism(stringUtil.stuListToString(signForm.getAbsenteeismStudents()));
    signForm.setTools(stringUtil.toolListToString(signForm.getToolList()));
    signForm.setRemarks(stringUtil.remarkListToString(signForm.getRemarkList()));
    signForm.setSuccessorId(signForm.getSuccessors().getSId());
    signFormMapper.updateSignForm(signForm);
  }

  @Override
  public boolean deleteSignForm(Integer sfId) {
    if (sfId == null){
      return false;
    }else {
      SignForm signForm = signFormMapper.getSignFormById(sfId);
      List<Integer> remarkIds = Arrays.stream(signForm.getRemarks().split(",")).map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
      remarkMapper.delete(remarkIds);
      signFormMapper.deleteSignForm(sfId);
      return true;
    }
  }

  @Override
  public boolean deleteSignForms(String sfIds) {
    if (sfIds==null||"".equals(sfIds)){
      return false;
    }else {
      List<Integer> sfIdList = Arrays.stream(sfIds.split(",")).map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
      for (Integer i : sfIdList) {
        SignForm sf = signFormMapper.getSignFormById(i);
        List<Integer> remarkIds = Arrays.stream(sf.getRemarks().split(",")).map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        remarkMapper.delete(remarkIds);
      }
      signFormMapper.deleteSignForms(sfIdList);
      return true;
    }
  }
}
